package com.softlab.retailwholesalesystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.softlab.retailwholesalesystem.entity.ClientEntity;
import com.softlab.retailwholesalesystem.entity.OrderEntity;
import com.softlab.retailwholesalesystem.mapper.ClientMapper;
import com.softlab.retailwholesalesystem.mapper.OrderMapper;
import com.softlab.retailwholesalesystem.service.IClientService;
import com.softlab.retailwholesalesystem.service.exception.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.List;
import java.util.UUID;

@Service
public class ClientServiceImpl implements IClientService {

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    ClientMapper mapper;


    @Override
    public void addClient(ClientEntity client) {
        int insert = mapper.insert(client);
        if (insert==0){
            throw new InsertRunTimeException("新增时出现异常");
        }
    }

    @Override
    public List<ClientEntity> searchClient(ClientEntity clientEntity) {
        QueryWrapper<ClientEntity> wrapper = new QueryWrapper<>();
        if (clientEntity.getClientId()!=null) wrapper.eq("client_id",clientEntity.getClientId());
        if (clientEntity.getClientName()!=null) wrapper.eq("client_name",clientEntity.getClientName());
        if (clientEntity.getSex()!=null) wrapper.eq("sex",clientEntity.getSex());
        if (clientEntity.getTelephone()!=null) wrapper.eq("telephone",clientEntity.getTelephone());
        if (clientEntity.getType()!=null) wrapper.eq("type",clientEntity.getType());
        if (clientEntity.getShopMark()!=null) wrapper.eq("shop_mark",clientEntity.getShopMark());
        wrapper.eq("is_delete",0);
//        return mapper.selectList(wrapper);

        List<ClientEntity> clientList = mapper.selectList(wrapper);
        for (ClientEntity client : clientList){
            QueryWrapper<OrderEntity> qw = new QueryWrapper<>();
            qw.eq("is_delete", 0);
            qw.eq("client_name", client.getClientName());

            List<OrderEntity> orderList = orderMapper.selectList(qw);
            Double money = 0.0;
            for (OrderEntity order : orderList){
                money += order.getTotalPrice();
            }
            client.setMoney(money);
        }

        return clientList;
    }

    @Override
    public List<ClientEntity> getAllClient() {
        QueryWrapper<ClientEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("is_delete",0);
        List<ClientEntity> clientList = mapper.selectList(wrapper);
        for (ClientEntity client : clientList){
            QueryWrapper<OrderEntity> qw = new QueryWrapper<>();
            qw.eq("is_delete", 0);
            qw.eq("client_name", client.getClientName());

            List<OrderEntity> orderList = orderMapper.selectList(qw);
            Double money = 0.0;
            for (OrderEntity order : orderList){
                money += order.getTotalPrice();
            }
            client.setMoney(money);
        }

        return clientList;
    }

    @Override
    public void updateClient(ClientEntity client) {
        int i = mapper.updateById(client);
        if (i==0){
            throw new UpdateErrorException("更新时遇到错误");
        }
    }

    @Override
    public void deleteClient(ClientEntity client) {
        client.setIsDelete(1);
        int i = mapper.updateById(client);
        if (i==0){
            throw new DeleteErrorException("删除时遇到未知错误");
        }
    }

    @Override
    public ClientEntity login(ClientEntity client) {
        QueryWrapper<ClientEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("telephone",client.getTelephone());
        wrapper.eq("is_delete",0);
        List<ClientEntity> clients = mapper.selectList(wrapper);
        if (clients.size() == 0){
            throw new NoUserException("用户名不存在");
        }
        ClientEntity c=clients.get(0);
        String password = client.getPassword();
        String salt = c.getSalt();
        password=md5Password(password,salt);
        if (!password.equals(c.getPassword())){
            throw new PasswordWrongException("用户名或密码错误");
        }
        c.setPassword(null);
        c.setSalt(null);
        return c;
    }

    @Override
    public void registerClient(ClientEntity client) {
        QueryWrapper<ClientEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("telephone",client.getTelephone());
        wrapper.eq("is_delete",0);
        ClientEntity existUser = mapper.selectOne(wrapper);
        if (existUser!=null){
            throw new UsernameDuplicatedException("该用户已存在");
        }
        String password = client.getPassword();
        String salt = UUID.randomUUID().toString().toUpperCase();
        client.setSalt(salt);
        client.setClientName(client.getTelephone());
        password=md5Password(password,salt);
        client.setPassword(password);
        int insert = mapper.insert(client);
        if (insert!=1){
            throw new InsertRunTimeException("信息注册错误");
        }
    }


    @Override
    public void changePassword(ClientEntity client) {
        QueryWrapper<ClientEntity> wrapper=new QueryWrapper<>();
        wrapper.eq("telephone",client.getTelephone());
        ClientEntity client1 = mapper.selectOne(wrapper);
        if(client1 == null){
            throw new NoUserException("无此用户");
        }
        String salt = client.getSalt();
        String password=client.getPassword();
        password=md5Password(password,salt);
        client.setPassword(password);
        client.setClientId(client1.getClientId());
        mapper.updateById(client);
    }

    public String md5Password(String password,String salt) {
        for (int i = 0; i < 3; i++) {
            password= DigestUtils.md5DigestAsHex((salt+password+salt).getBytes()).toUpperCase();
        }
        return password;
    }


}
